package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @autahor: csl
 * @date: 2019/7/23
 * @description: 预约实现类
 * @version:1.0
 */
/*
1. 检查预约日期是否做预约设置
   1. 根据日期获取预约设置对象
   2. 预约设置对象为空，则没有做预约设置
2. 检查预约日期是否已经约满
3. 检查用户是否为会员
   1. 根据手机号获取会员信息
   2. 如果会员不为空 ，则是会员
      如果是会员，根据会员ID、预约日期、套餐ID进行查询，看是否有预约记录，如果有直接返回，没有继续执行
   3. 如果会员为空，不是会员，则自动注册会员
4. 保存预约订单，预约成功
   封装订单对象，包含会员Id 、预约日期、订单类型、订单状态、套餐ID
5. 更新预约设置，更新预约人数
   获取当前预约人数，在现有人数上加1。

 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public Result submit(Map<String, String> map) {

     /*
         1. 检查预约日期是否做预约设置

         1. 根据日期获取预约设置对象
        2. 预约设置对象为空，则没有做预约设置
     */
        Date orderDate = null;
      try {
          orderDate = DateUtils.parseString2Date(map.get("orderDate"));
      }catch (Exception e){
          e.printStackTrace();
      }
          //根据日期查询ordersetting
          OrderSetting orderSetting=  orderSettingDao.findOrderSettingByOrderDate(orderDate);
          //如果orderSetting不为空，说明设置过预约人数
          if (orderSetting==null){
              return new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
          }
          //判断预约人数是否预约满了
          if (orderSetting.getReservations() >=orderSetting.getNumber()){
              return new Result(false,MessageConst.ORDER_FULL);
          }
      //检查是否是会员
          String telephone = map.get("telephone");
          Member member= memberDao.findMemberByTelephone(telephone);

              if (member==null){
                  //注册会员
                  member = new Member();
                  member.setIdCard(map.get("idCard"));
                  member.setName(map.get("name"));
                  member.setPhoneNumber(telephone);
                  member.setSex(map.get("sex"));
                  member.setRegTime(new Date());
                  memberDao.addMember(member);
              }else {
                  //是会员，根据会员ID、预约日期、套餐ID进行查询，看是否有预约记录，如果有直接返回，没有继续执行
                  Integer memberId = member.getId();
                  Order order = new Order();
                  order.setMemberId(memberId);
                  order.setOrderDate(orderDate);
                  order.setSetmealId(Integer.parseInt(map.get("setmealId")));

                  List<Order>list= orderDao.findByOrder(order);

                  if (list!=null &&list.size()>0){
                      //已预约过
                      return new Result(false,MessageConst.HAS_ORDERED);
                  }
              }


          Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderDate);
            order.setOrderType(map.get("orderType"));
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setSetmealId(Integer.parseInt(map.get("setmealId")));
            orderDao.add(order);
            //保存成功后修改orderSetting的已预约人数
          OrderSetting setting = new OrderSetting();
          setting.setReservations(setting.getReservations()+1);
          setting.setOrderDate(orderDate);
          orderSettingDao.updateByOrderDate(setting);
          return new Result(true,MessageConst.ORDER_SUCCESS,order.getId());

    }

    @Override
    public Map<String,Object> findById(Integer id) {
         try {
        Map<String,Object> map= orderDao.findById(id);
        map.put("orderDate",DateUtils.parseDate2String((Date) map.get("orderDate")));
        return map;

                 }catch (Exception e){
                     e.printStackTrace();
             return new HashMap<String, Object>();
                 }
    }
}
